package cn.heima.com;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo3 {
    public static void main(String[] args) {
        File file  = new File("D:\\Thunder");
        HashMap<String, Integer> mp = new HashMap<>();
        //getConut(mp,file);
        //System.out.println(mp);
        getSecondConut(mp,file);
       for (Map.Entry<String, Integer> map:mp.entrySet()){
           System.out.println(map.getKey()+"==>"+map.getValue());
       }
    }

    private static void getSecondConut(HashMap<String, Integer> mp, File file) {
        if (file==null || file.isFile()){
            return;
        }
        File[] listFiles = file.listFiles();
        for (File listFile: listFiles){
            if(listFile.isFile()){
                String listFileName = listFile.getName();
                String[] splitName = listFileName.split("\\.");
                String suffixName = splitName[splitName.length - 1];
                mp.put(suffixName,mp.containsKey(suffixName)?mp.get(suffixName)+1:1);
            }else {
                getSecondConut(mp,listFile);
            }
        }
    }

    private static void getConut(HashMap<String, Integer> mp, File file) {
        if(file==null || file.isFile()){
            return;
        }
        File[] listFiles = file.listFiles();
        for (File files:listFiles){
            if (files.isFile()){
                String filesName = files.getName();
                String[] splitName = filesName.split("\\.");
                if(splitName.length==2){
                    String s = splitName[1];
                    if(mp.containsKey(s)){
                        Integer count = mp.get(s);
                        count++;
                        mp.put(s,count++);
                    }else{
                        mp.put(s,1);
                    }
                }
            }else {
                getConut(mp,files);
            }

        }
    }

}
